// pages/newmore/newmore.js
var utils = require('../../utils/util.js');
var httpUrl = getApp().globalData.httpUrl;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        currentPage: 1,
        slideList:[],
        detail:""
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.setData({
            type:options.type
        })
        var that = this;
        if (options.type == 1) {
            this.newList("/mobile/notice/index");
            wx.setNavigationBarTitle({
                title: '新闻',
            })
        } else if (options.type == 2) {
            this.newList("/mobile/article/article_list");
            wx.setNavigationBarTitle({
                title: '热推文章',
            })
        } else if (options.type == 4) {
            this.newList("/mobile/coach/article");
            wx.setNavigationBarTitle({
                title: '兴趣文章',
            })
        }
    },
    //列表展示
    newList: function (typeUrl) {
        var that = this;
        var http=httpUrl + typeUrl;
        utils.http(http, { page:that.data.currentPage}, function(res) {
            if (res.state == 1) {
                console.log(res.datas)
                var dataList = that.data.slideList;
                if (that.data.slideList==undefined){
                    dataList = [];
                }
                for (var i in res.datas) {
                    var time = Date.parse(new Date(res.datas[i].create_time));
                    var timeData1 =utils.getPastTime(time);
                    var port = {
                        id: res.datas[i].id,
                        title: res.datas[i].title,
                        photo: res.datas[i].photo,
                        reading: res.datas[i].reading,
                        col_num: res.datas[i].col_num,
                        zan_num: res.datas[i].zan_num,
                        create_time: timeData1
                    }
                    dataList.push(port);
                    if (res.datas.length<10){
                        that.setData({
                            tishi_title: '已加载全部',
                        });
                    }else{
                        that.setData({
                            tishi_title: '加载更多',
                        });
                    }
                }
                that.setData({
                    slideList: dataList,
                    ready: res.datas.length >=0 ? 1 : 2,
                });
                console.log(that.data.slideList);
            } else {
                utils.message(res.datas);
            }
        })
    },

    //进入新闻详情
    noNewmoreDetailTap: function (e) {
        var that=this;
        wx.navigateTo({
            url: '../newmore/newmoreDetail/newmoreDetail?id=' + e.currentTarget.dataset.id +"&type="+that.data.type,
        })
    },

    //上拉加载更多
    onReachBottom: function () {
        var that = this;
        if (that.data.ready==1){
            var currentPage = that.data.currentPage;
            currentPage++;
            that.setData({
                currentPage: currentPage
            })
            if (that.data.type == 1) {
                this.newList("/mobile/notice/index");
            }else if (that.data.type == 2) {
                this.newList("/mobile/article/article_list");
            }
        }
        

    },
})